#!/usr/bin/env python3
"""
A do-not-disturb button for muting Dunst notifications in i3 using i3blocks

Mute is handled using the `dunstctl` command.
"""

__author__ = "Jessey White-Cinis <j@cin.is>"
__copyright__ = "Copyright (c) 2019 Jessey White-Cinis"
__license__ = "MIT"
__version__ = "1.1.0"

import os
import subprocess
import json

def mute_toggle():
    '''Toggle dunst notifications'''
    subprocess.run(["dunstctl", "set-paused", "toggle"], check=True)

def clicked():
    '''Returns True if the button was clicked'''
    button = os.environ.get("BLOCK_BUTTON", None)
    return bool(button)

def muted():
    '''Returns True if Dunst is muted'''
    cmd = ['dunstctl', 'is-paused']
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    output = proc.communicate()[0]
    return u'true' ==  output.strip().decode("UTF-8")

if clicked():
    # toggle button click to turn mute on and off
    mute_toggle()

if muted():
    RTN = {"full_text":"<span font='Font Awesome 5 Free Solid' color='#BE616E'>\uf1f6</span>"}
else:
    RTN = {"full_text":"<span font='Font Awesome 5 Free Solid' color='#A4B98E'>\uf0f3</span>"}

print(json.dumps(RTN))
